﻿/**
*    @file:    widget.cpp
*    @brief:
*    @author:  sanshi522@163.com
*    @date:    2025-04-07  18:00
*/
#include "widget.h"
#include "ui_widget.h"

#include <QDebug>
#include <QFileDialog>
#include <qglobalconfig.h>
#include <servicesettingdialog.h>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->installBtn->setDisabled(true);
    ui->switchBtn->setDisabled(true);
    ui->settingBtn->setDisabled(true);

    QGlobalConfig::getInstance().init(QCoreApplication::applicationDirPath() + "/config.ini");

    QString titleName = QGlobalConfig::getInstance().read("serviceManage", "titleName", "服务管理器").toString();
    setWindowTitle(titleName);

    QString serverName = QGlobalConfig::getInstance().read("serviceManage", "serverName", "").toString();
    QString serverDescription = QGlobalConfig::getInstance().read("serviceManage", "serverDescription", "").toString();

    ui->serverName->setText(serverName);
    ui->serverDescription->setText(serverDescription);

    controlThread = new ServiceControlThread();
    connect(this, &Widget::signal_startService, controlThread, &ServiceControlThread::slot_startService);
    connect(this, &Widget::signal_stopService, controlThread, &ServiceControlThread::slot_stopService);
    connect(this, &Widget::signal_installService, controlThread, &ServiceControlThread::slot_installService);
    connect(this, &Widget::signal_uninstallService, controlThread, &ServiceControlThread::slot_uninstallService);
    connect(controlThread, &ServiceControlThread::signal_serviceState, this, &Widget::slot_serviceState);
}

Widget::~Widget()
{
    delete controlThread;
    QGlobalConfig::getInstance().uninit();
    delete ui;
}

void Widget::slot_serviceState(int state, QString message)
{
    serverState = state;
    switch (serverState)
    {
    case -2:
        ui->serviceState->setText(message);
        break;
    case -1:
        ui->serviceState->setText(QString("服务未安装！"));
        break;
    case SERVICE_RUNNING:
        ui->serviceState->setText(QString("服务已启动！"));
        break;
    case SERVICE_STOPPED:
        ui->serviceState->setText(QString("服务已停止！"));
        break;
    }
    ui->installBtn->setDisabled(serverState == -2);
    ui->settingBtn->setDisabled(serverState == SERVICE_RUNNING);
    ui->installBtn->setText(serverState == -1 ? QString("安装") : QString("卸载"));
    ui->switchBtn->setDisabled(serverState == -2 || serverState == -1);
    ui->switchBtn->setText(serverState == SERVICE_RUNNING ? QString("停止") : QString("启动"));
}

void Widget::on_installBtn_clicked()
{
    qDebug() << __FUNCTION__ << serverState;
    if (serverState == -2)
        return;
    if (serverState == -1)
    {
        signal_installService();
    }
    else
    {
        signal_uninstallService();
    }
}

void Widget::on_switchBtn_clicked()
{
    if (serverState == SERVICE_RUNNING)
    {
        signal_stopService();
    }
    if (serverState == SERVICE_STOPPED)
    {
        signal_startService();
    }
}

void Widget::on_serverName_editingFinished()
{
    QGlobalConfig::getInstance().write("serviceManage", "serverName", ui->serverName->text());
    QGlobalConfig::getInstance().push();
}

void Widget::on_serverDescription_editingFinished()
{
    QGlobalConfig::getInstance().write("serviceManage", "serverDescription", ui->serverDescription->text());
    QGlobalConfig::getInstance().push();
}

void Widget::on_settingBtn_clicked()
{
    ServiceSettingDialog dialog;
    dialog.exec();
}
